#
#  Copyright 2022 The Open Islands Authors. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#
import os

from pyoi.constant import StorageEngine
from pyoi.runtime_env import RuntimeENV


class UriUtils:
    @classmethod
    def get_storage_engine_from_uri(cls, uri: str):
        for engine in StorageEngine:
            if uri.startswith(engine.value):
                return engine
        else:
            raise RuntimeError(f"can not get storage engine type from uri {uri}")

    @classmethod
    def trim_file_uri(cls, uri: str) -> str:
        return uri.replace(StorageEngine.LOCALFS.value, "")

    @classmethod
    def fill_abs_file_uri(cls, uri: str) -> str:
        if uri.startswith("./"):
            return "file://" + os.path.join(RuntimeENV.OI_PROJECT_ROOT, uri)
        else:
            return uri
